<?php

namespace app\command;

use app\model\AgentShop;
use app\model\Talent;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Log;
use Wechat\CommerceApp;

class ShopCommand extends Command
{
    protected function configure()
    {
        $this->setName('shop')
            ->setDescription('获取合作小店列表');
    }

    protected function execute(Input $input, Output $output)
    {
        Log::init([
            'type'        => 'file',
            'apart_level' => ['error'],
            'level'       => ['error'],
            'path'        => app()->getRootPath() . "runtime/log/shop/",
            'max_files'   => 5,
        ]);
        try {
            $next_key = '';
            do {
                [$lists, $next_key, $has_more] = CommerceApp::getInstance()->getShopList($next_key);
                $next_key = $next_key ?? '';
                $has_more = $has_more ?? false;

                $ids = $save = [];
                foreach ($lists as $item) {
                    $ids[] = $item['base_info']['appid'];
                }
                $db_lists = AgentShop::where('appid', 'in', $ids)->column('appid, id, status');

                foreach ($lists as $item) {
                    $base_info = $item['base_info'];
                    $appid     = $base_info['appid'];
                    $shop_info = CommerceApp::getInstance()->getShopInfo($appid);
                    if (!isset($db_lists[$appid])) {
                        $save[] = [
                            'appid'          => $appid,
                            'name'           => $base_info['nickname'],
                            'image'          => $base_info['headimg_url'] ?? '',
                            'agent_id'       => 0,
                            'status'         => $item['status'],
                            'product_number' => $shop_info['data_info']['product_number'] ?? 0,
                            'settle_amount'  => $shop_info['data_info']['settle_amount'] ?? 0,
                            'approved_time'  => $shop_info['approved_time'] ?? 0,
                        ];
                    } else {
                        $save[] = [
                            'id'             => $db_lists[$appid]['id'],
                            'status'         => $item['status'],
                            'product_number' => $shop_info['data_info']['product_number'] ?? 0,
                            'settle_amount'  => $shop_info['data_info']['settle_amount'] ?? 0,
                            'approved_time'  => $shop_info['approved_time'] ?? 0,
                        ];
                    }
                }
                if ($save) {
                    (new AgentShop())->saveAll($save);
                }
            } while ($has_more);
        } catch (\Exception $e) {
            $log = [
                'msg'   => '获取合作小店列表失败',
                'error' => $e->getMessage(),
                'file'  => $e->getFile(),
                'line'  => $e->getLine(),
            ];
            Log::error($log);
            exit;
        }

    }
}
